local cjson = require "cjson"
local dyups = require "ngx.dyups"
local redis = require "resty.redis"
local serverName = ngx.var.target
 
function getAndUpdateServerList()
  --[[ local redis = redis:new()
   redis:set_timeout(1000) -- 1 sec
   local ok, err = redis:connect("127.0.0.1", 6379) --]]

   local rc = require("resty.redis.connector").new({
                connect_timeout = 50,
                read_timeout = 5000,
                keepalive_timeout = 30000,
                password = "yanlei",
               
            })
     redis, err = rc:connect{
             url = "sentinel://yanlei@mymaster:m",
             sentinels = {
                { 
                host = "127.0.0.1", port = 26379,
                host = "127.0.0.1", port = 26380,
                host = "127.0.0.1", port = 26381   
                },
            }
          }

    -- 使用获取redis里的upstream列表信息  为了在nginx里更新
    local serverList = redis:hget(serverName,"serverList")
  --[[  local keyObj = cjson.decode(serverList) --]]
      
    ngx.log(ngx.ERR, "upStream ServerList are2222: ", serverList)
    if serverList == ngx.null then
       ngx.say("{code:500,msg:serverList in "..serverName.." in redis have nil value!}") 
       return
    else   

      local status, rv = dyups.update(serverName,serverList);
      if status ~= ngx.HTTP_OK then
         ngx.log(ngx.ERR, "err status",status) 
         ngx.say("{code:500,msg:dyups.update error occurrd!}")  
        return
      else 
        ngx.say("{code:200,msg:updated success}")  
      end   
    end        
    
end




getAndUpdateServerList()

